package com.voxel.sense.infomsg.view.decoration;

import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.RecyclerView;
import android.view.View;

import cyy.example.com.facotry.Factory;


public class ItemDivider extends RecyclerView.ItemDecoration {

    private Drawable mDivider;

    public ItemDivider(int resId) {
        mDivider = Factory.app().getResources().getDrawable(resId);

    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent) {
        //        final int left = parent.getPaddingLeft();
        //        final int right = parent.getWidth() - parent.getPaddingRight();
        //
        //        final int childCount = parent.getChildCount();
        //        for (int i = 0; i < childCount; i++) {
        //            final View child = parent.getChildAt(i);
        //            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child
        //                    .getLayoutParams();
        //            //以下计算主要用来确定绘制的位置
        //            final int top = child.getBottom() + params.bottomMargin;
        //            final int bottom = top + mDrawable.getIntrinsicHeight();
        //            mDrawable.setBounds(left, top, right, bottom);
        //            mDrawable.draw(c);
        //        }
    }

    @Override
    public void onDraw(Canvas c, RecyclerView parent, RecyclerView.State state) {
        super.onDraw(c, parent, state);
        drawHorizontalLine(c, parent, state);
    }

    //画横线, 这里的parent其实是显示在屏幕显示的这部分
    public void drawHorizontalLine(Canvas c, RecyclerView parent, RecyclerView.State state) {
        int left = parent.getPaddingLeft();
        int right = parent.getWidth() - parent.getPaddingRight();
        final int childCount = parent.getChildCount();
        for (int i = 0; i < childCount; i++) {
            final View child = parent.getChildAt(i);

            //获得child的布局信息
            final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();
            final int top = child.getBottom() + params.bottomMargin;
            final int bottom = top + mDivider.getIntrinsicHeight();
            mDivider.setBounds(left, top, right, bottom);
            mDivider.draw(c);
            //Log.d("wnw", left + " " + top + " "+right+"   "+bottom+" "+i);
        }
    }


    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        outRect.set(0, 0, 0, mDivider.getIntrinsicHeight());
    }
}
